package com.welink.cmp.adapter.api.metadata;

import com.welink.base.catchlog.CatchAndLog;
import com.welink.base.exception.BizException;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.DestroyUpdateCmd;
import com.welink.cmp.dto.metadata.router.*;
import com.welink.cmp.meatdata.service.RouterAppService;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 路由器生命周期管理接口
 *
 * @create
 */
@RestController("/api/router")
@CatchAndLog
public class RouterLifeCycleServiceApi {

    @Autowired
    private RouterAppService routerAppService;

    /**
     * 创建路由器
     *
     * @param cmd 创建路由器指令
     * @return 路由器ID
     */
    @PostMapping("/addRouter")
    public SingleResponse<Long> addRouter(@RequestBody CreateRouterAddCmd cmd) {
        final Long id = routerAppService.addRouter(cmd);
        return SingleResponse.of(id);
    }

    /**
     * 上架路由器
     *
     * @param cmd 上架路由器指令
     * @return 操作结果
     */
    @PostMapping("/launchRouter")
    public Response launchRouter(@RequestBody LaunchRouterUpdateCmd cmd) {
        // TODO 状态机
        final Boolean result = routerAppService.launchRouter(cmd);
        if (!result){
            throw new BizException("上架路由器异常");
        }
        return Response.buildSuccess();
    }

    /**
     * 配置路由器基础信息
     *
     * @param cmd 配置路由器基础信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/routerBase")
    public Response setupRouterBaseInfo(@RequestBody SetupRouterBaseUpdateCmd cmd) {
        final Boolean b = routerAppService.setupRouterBaseInfo(cmd);
        return Response.buildSuccess();
    }

    /**
     * 配置路由器网络信息
     *
     * @param cmd 配置路由器网络信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/routerNet")
    public Response setupRouterNetInfo(@RequestBody SetupRouterNetUpdateCmd cmd) {
        final Boolean b = routerAppService.setupRouterNetInfo(cmd);
        return Response.buildSuccess();
    }

    /**
     * 配置路由器物理机房信息
     *
     * @param cmd 配置路由器物理机房信息指令
     * @return 操作结果
     */
    @PostMapping("/setup/routerRack")
    public Response setupRouterRackInfo(@RequestBody SetupRouterRackUpdateCmd cmd) {
        final Boolean b = routerAppService.setupRouterRackInfo(cmd);
        return Response.buildSuccess();
    }

    /**
     * 下架路由器
     *
     * @param cmd 下架路由器指令
     * @return 操作结果
     */
    @PostMapping("/shutdownRouter")
    public Response shutdownRouter(@RequestBody ShutdownRouterUpdateCmd cmd) {
        final Boolean b = routerAppService.shutdownRouter(cmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"下架路由器指令发生异常");
        }
        return Response.buildSuccess();
    }

    /**
     * 注销路由器
     *
     * @param cmd 注销路由器指令
     * @return 操作结果
     */
    @PostMapping("/destroyRouter")
    public Response destroyRouter(@RequestBody DestroyUpdateCmd cmd) {
        // TODO  状态机
        return Response.buildSuccess();
    }
}